//
//  SDSettingViewController.m
//  Sydeny
//
//  Created by Rainbow on 8/14/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "SDSettingViewController.h"
#import "MyMailViewController.h"
#import "ContactUsViewController.h"
#import "AboutUsViewController.h"
#import "ServiceHelper.h"

@implementation SDSettingViewController
@synthesize settingBgView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc
{
    [settingBgView release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    settingBgView.layer.cornerRadius = 12;
    
    UIImageView *logoView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"logo.png"]] autorelease];
    logoView.frame = CGRectMake(0, 0, 94, 29);
    self.navigationItem.titleView = logoView;
    
    
    UIButton *backBtn = [[[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 30)] autorelease];
    [backBtn setBackgroundImage:[UIImage imageNamed:@"back_btn.png"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(tapBackBtn) forControlEvents:UIControlEventTouchUpInside];
    [backBtn setTitle:@"返回" forState:UIControlStateNormal];
    [backBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, 5, 0, 0)];
    backBtn.titleLabel.font = [UIFont boldSystemFontOfSize:12];
    UIBarButtonItem *backItem = [[[UIBarButtonItem alloc] initWithCustomView:backBtn] autorelease];
    self.navigationItem.leftBarButtonItem = backItem;
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    [self setSettingBgView:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil] autorelease];
    if (indexPath.row == 0) {
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.textLabel.text = @"推送设置";
        UISwitch *switcher = [[UISwitch alloc] initWithFrame:CGRectMake(190, 8, 97, 27)];
        [switcher setOn:[[NSUserDefaults standardUserDefaults] boolForKey:US_PUSH_NOTIFICATION]];
        [switcher addTarget:self action:@selector(pushSettingChanged:) forControlEvents:UIControlEventValueChanged];
        [cell addSubview:switcher];
        [switcher release];
    }
    else if (indexPath.row == 1) {
        cell.textLabel.text = @"联系我们";
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    else
    {
        cell.textLabel.text = @"关于我们";
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 1) {
//        NSString *subject = @"意见反馈";
//        NSString *recipient = @"jamerainbow@gmail.com";
//        MyMailViewController *mailController = [[[MyMailViewController alloc] init] autorelease];
//        mailController.navigationBar.barStyle = UIBarStyleBlackOpaque;
//        [mailController setSubject:subject];
//        [mailController setToRecipients:[NSArray arrayWithObject:recipient]];
//        [self presentModalViewController:mailController animated:YES];
        ContactUsViewController *contactUsViewController = [[[ContactUsViewController alloc] init] autorelease];
        [self.navigationController pushViewController:contactUsViewController animated:YES];
        
    }
    else if(indexPath.row == 2)
    {
        
        AboutUsViewController *aboutUsViewController = [[[AboutUsViewController alloc] init] autorelease];
        [self.navigationController pushViewController:aboutUsViewController animated:YES];
    }
    
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (IBAction)pushSettingChanged:(id)sender {
    UISwitch *switcher = (UISwitch *)sender;
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setBool:switcher.on forKey:US_PUSH_NOTIFICATION];
    
    if ([[NSUserDefaults standardUserDefaults] stringForKey:DEVICE_TOKEN] != nil && ![@"" isEqualToString:[[NSUserDefaults standardUserDefaults] stringForKey:DEVICE_TOKEN]]) {
        NSMutableDictionary *params = [[NSMutableDictionary alloc] initWithCapacity:0];
        if (switcher.on) {
            [params setObject:@"1" forKey:@"state"];
        }
        else
        {
            [params setObject:@"0" forKey:@"state"];
        }
        [params setObject:[[NSUserDefaults standardUserDefaults] stringForKey:DEVICE_TOKEN] forKey:@"token"];
        [[ServiceHelper sharedService] requestServerForMethod:SydenyRequestSetNotification WithParameters:params AndCallBackScope:nil AndSuccessSel:nil AndFailedSel:nil];
    }
}

-(void)tapBackBtn
{
    [self.navigationController popViewControllerAnimated:YES];
}

@end
